﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Ultra_Presenter.Model;

namespace Ultra_Presenter.UC
{
    /// <summary>
    /// Interaction logic for Layout_3.xaml
    /// </summary>
    public partial class Layout_3 : UserControl
    {
        /// <summary>
        /// Click event
        /// </summary>
        public event MouseLeftButtonUpEventHandler ChangedA;

        /// <summary>
        /// Click event
        /// </summary>
        public event MouseLeftButtonUpEventHandler ChangedB;

        /// <summary>
        /// Click event
        /// </summary>
        public event MouseLeftButtonUpEventHandler ChangedC;

        /// <summary>
        /// Private URL List.
        /// </summary>
        private List<Url> urlQuadrantA;

        /// <summary>
        /// Private URL List.
        /// </summary>
        private List<Url> urlQuadrantB;

        /// <summary>
        /// Private URL List.
        /// </summary>
        private List<Url> urlQuadrantC;

        /// <summary>
        /// Private var quadrant.
        /// </summary>
        private Quadrant quadrantA;

        /// <summary>
        /// Private var quadrant.
        /// </summary>
        private Quadrant quadrantB;

        /// <summary>
        /// Private var quadrant.
        /// </summary>
        private Quadrant quadrantC;

        /// <summary>
        /// Class constructor.
        /// </summary>
        public Layout_3()
        {
            this.InitializeComponent();
            this.ResetControl();
        }

        /// <summary>
        /// Reset the control.
        /// </summary>
        public void ResetControl()
        {
            this.urlQuadrantA = new List<Url>();
            this.urlQuadrantB = new List<Url>();
            this.urlQuadrantC = new List<Url>();

            this.quadrantA = new Quadrant { Name = QuadrantName.A };
            this.quadrantB = new Quadrant { Name = QuadrantName.B };
            this.quadrantC = new Quadrant { Name = QuadrantName.C };
        }

        /// <summary>
        /// Add new URL.
        /// </summary>
        /// <param name="url">URL object.</param>
        /// <param name="nome">Quadrant name.</param>
        public void AddURL(Url url, QuadrantName nome)
        {
            switch (nome)
            {
                case QuadrantName.A:
                    this.urlQuadrantA.Add(url);
                    this.quadrantA.Urls = this.urlQuadrantA.ToArray();
                    break;
                case QuadrantName.B:
                    this.urlQuadrantB.Add(url);
                    this.quadrantB.Urls = this.urlQuadrantB.ToArray();
                    break;
                case QuadrantName.C:
                    this.urlQuadrantC.Add(url);
                    this.quadrantC.Urls = this.urlQuadrantC.ToArray();
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Add new URL.
        /// </summary>
        /// <param name="url">URL object.</param>
        /// <param name="nome">Quadrant name.</param>
        public void AddURL(Url[] url, QuadrantName nome)
        {
            switch (nome)
            {
                case QuadrantName.A:
                    this.urlQuadrantA = url.ToList();
                    this.quadrantA.Urls = url;
                    break;
                case QuadrantName.B:
                    this.urlQuadrantB = url.ToList();
                    this.quadrantB.Urls = url;
                    break;
                case QuadrantName.C:
                    this.urlQuadrantC = url.ToList();
                    this.quadrantC.Urls = url;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Remove the URL.
        /// </summary>
        /// <param name="nome">Quadrant name.</param>
        /// <param name="index">Index of line.</param>
        public void RemoveURL(QuadrantName nome, int index)
        {
            switch (nome)
            {
                case QuadrantName.A:
                    this.urlQuadrantA.RemoveAt(index);
                    this.quadrantA.Urls = this.urlQuadrantA.ToArray();
                    break;
                case QuadrantName.B:
                    this.urlQuadrantB.RemoveAt(index);
                    this.quadrantB.Urls = this.urlQuadrantB.ToArray();
                    break;
                case QuadrantName.C:
                    this.urlQuadrantC.RemoveAt(index);
                    this.quadrantC.Urls = this.urlQuadrantC.ToArray();
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Sets the quadrant.
        /// </summary>
        /// <param name="quadrant">Object Quadrant.</param>
        public void SetQuadrant(Quadrant quadrant)
        {
            switch (quadrant.Name)
            {
                case QuadrantName.A:
                    this.quadrantA = quadrant;
                    
                    if (quadrant.Urls != null)
                    {
                        this.urlQuadrantA = quadrant.Urls.ToList();
                    }

                    break;
                case QuadrantName.B:
                    this.quadrantB = quadrant;
                    
                    if (quadrant.Urls != null)
                    {
                        this.urlQuadrantB = quadrant.Urls.ToList();
                    }

                    break;
                case QuadrantName.C:
                    this.quadrantC = quadrant;

                    if (quadrant.Urls != null)
                    {
                        this.urlQuadrantC = quadrant.Urls.ToList();
                    }

                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Get quadrant list.
        /// </summary>
        /// <returns>Get list of quadrant.</returns>
        public Quadrant[] GetQuadrants()
        {
            List<Quadrant> q = new List<Quadrant>();
            q.Add(this.quadrantA);
            q.Add(this.quadrantB);
            q.Add(this.quadrantC);

            return q.ToArray();
        }

        /// <summary>
        /// Get one quadrant.
        /// </summary>
        /// <param name="nome">Quadrant name.</param>
        /// <returns>Object quadrant.</returns>
        public Quadrant GetQuadrant(QuadrantName nome)
        {
            switch (nome)
            {
                case QuadrantName.A:
                    return this.quadrantA;
                case QuadrantName.B:
                    return this.quadrantB;
                case QuadrantName.C:
                    return this.quadrantC;
                default:
                    return null;
            }
        }

        /// <summary>
        /// Get URL list.
        /// </summary>
        /// <param name="nome">Quadrant name.</param>
        /// <returns>URL List.</returns>
        public Url[] GetURLs(QuadrantName nome)
        {
            switch (nome)
            {
                case QuadrantName.A:
                    return this.urlQuadrantA.ToArray();
                case QuadrantName.B:
                    return this.urlQuadrantB.ToArray();
                case QuadrantName.C:
                    return this.urlQuadrantC.ToArray();
                default:
                    return null;
            }
        }

        /// <summary>
        /// Method to get the total time of this control.
        /// The sum of each URL.
        /// </summary>
        /// <returns>Returns the total time.</returns>
        public int GetTotalTime()
        {
            List<int> sumTime = new List<int>();

            sumTime.Add(this.urlQuadrantA.Sum(x => x.Time));
            sumTime.Add(this.urlQuadrantB.Sum(x => x.Time));
            sumTime.Add(this.urlQuadrantC.Sum(x => x.Time));

            return sumTime.Max();
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L3A_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.ChangedA != null)
            {
                this.ChangedA(this, e);
            }
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L3B_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.ChangedB != null)
            {
                this.ChangedB(this, e);
            }
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L3C_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.ChangedC != null)
            {
                this.ChangedC(this, e);
            }
        }
    }
}
